package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.entity.Product;
import com.nercel.dsj.gksales.service.ProductService;
import com.nercel.dsj.gksales.util.ApiResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Api(tags="商品管理")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductApiController extends BaseApiController {

    private static Logger logger = LoggerFactory.getLogger(ProductApiController.class);

    private final ProductService productService;

    @ApiOperation(value = "查询")
    @RequestMapping(value="/api/v1/product", method= RequestMethod.GET)
    public List<Product> list(
            @ApiParam(hidden = true) QueryParams queryParams,
            @ApiParam(value = "页码") @RequestParam(required = false) Integer page,
            @ApiParam(value = "单页数量") @RequestParam(required = false) Integer pageSize) {
        return productService.list(queryParams);
    }

    @ApiOperation(value = "根据UID查询")
    @RequestMapping(value="/api/v1/product/{uid}", method= RequestMethod.GET)
    public Product findByUid(@PathVariable String uid) {
        return productService.findByUid(uid);
    }

    @PostMapping(value = "/api/v1/product")
    @ApiOperation(value = "新增")
    public Product create(@ApiParam(value = "信息") @RequestBody Product product) {
        productService.save(product);
        return product;
    }

    @PutMapping(value = "/api/v1/product")
    @ApiOperation(value = "更新")
    public Product update(@ApiParam(value = "信息") @RequestBody Product product) {
        productService.update(product);
        return product;
    }

    @DeleteMapping(value = "/api/v1/product/{id}")
    @ApiOperation(value = "删除")
    public Object delete(@ApiParam(value = "ID") @PathVariable Integer id) {
        productService.delete(id);
        return ApiResultUtil.success();
    }

}
